<template>
    <div class="playlist">
        <div class="listDetail" v-if="playList">
            <div class="top">
                <span class="back iconfont icondirection-left"  @click="back"></span>
                <span class="like iconfont " :class="[ likeState ? 'iconicon_xihuan' : 'iconxihuan1']" @click="like"></span>
                <router-link class="comment iconfont iconpinglun" tag="span" :to="`/comment/${listID}`"></router-link>
            </div>
            <div class="playImg" :style="{backgroundImage: 'url( '+ playList.coverImgUrl +')'}" ></div>
            <div class="main">
                <div class="left">
                    <img :src="playList.coverImgUrl" alt="">
                    <span class="icon">歌单</span>
                    <i class="num">{{playList.playCount|formatNum}}万</i>
                </div>
                <div class="right">
                    <h2>{{playList.name}}</h2>
                    <div class="auth">
                        <img :src="playList.creator.avatarUrl" alt="">
                        {{playList.creator.nickname}}
                    </div>
                    <div class="all iconfont iconquanbu" @click="allPlay(songDetail)">&nbsp;&nbsp;全部播放</div>
                    <div class="tips" ref="tips">已经为您加入播放列表</div>
                </div>
            </div>
        </div>
        <div class="playSong">
            <h3 class="SongTitle">歌曲列表</h3>
            <ul class="newsong" v-if="songDetail.length>0">
                <li v-for="(s,index) in songDetail" :key="index">
                    <SongItem :item="s"><div class="musicIndex">{{index+1}}</div></SongItem>
                </li>
            </ul>
             <Loading v-if="isLoading"></Loading>
             <div class="loadMore" @click="addMore" v-show="!isLoading">{{ nomore ? "已经到底咯" : "点击加载更多..." }}</div>
        </div>
    </div>
</template>
<script>
import SongItem from '@/components/SongItem'
import Loading from '@/components/Loading'
export default {
    data(){
        return {
            playList:null,
            listID:null,
            songDetail: [],
            page: 1,
            isLoading: true,
            nomore: false,
            likeState: null,
            songTotla: 0
        }    
    },
    filters:{
        formatNum(val){
            return (val/10000).toFixed(1);
        }
    },
    created(){
        this.listID=this.$route.params.id;
        let likeList = JSON.parse( window.localStorage.getItem('likePlayList'));
        // 判断本地缓存有没有喜欢的数据
        if(likeList){
            this.likeState = false;
            likeList.forEach( item => {
                if(item == this.listID){
                    this.likeState = true;
                    return false;
                }
            });
        }else{
            this.likeState = false;
        }
        this.loadMusic();
        window.addEventListener("scroll", this.handleScroll);
    },
    methods:{
        loadMusic(){
            //获取歌单详情
            this.$http.get('/playlist/detail?id=' + this.listID).then(data=>{
                //把tracksID变成字符串
                this.songTotla = data.data.playlist.trackIds.length;
                let trackIDS=data.data.playlist.trackIds.slice((this.page-1)*10,this.page*10);
                this.playList=data.data.playlist;
                let ids=trackIDS.map(d=>{
                        return d.id;
                    })
                return ids.join(',');
            }).then(ids=>{
                // console.log(ids);
                //获取歌曲详情
                this.$http.get('/song/detail?ids='+ids).then(songData=>{
                    this.isLoading = false;
                    songData.data.songs.map(d=>{
                        //处理数据和 SongItem不一致 转换数据
                        d.song={
                            artists:d.ar
                        }
                        this.songDetail.push(d);
                        return d;
                    })
                });
            }).catch(err=>{
                console.log(err);
            });
        },
        addMore(){
            this.page++;
            // console.log(this.page);
            // console.log(this.songTotla);
            if (this.page>Math.ceil(parseInt(this.songTotla)/10)) {
                    //如果大于总页数停止请求数据
                    this.nomore = true;
                    return false;
                } else {
                    this.loadMusic();
                    this.isLoading = true;
                }
        },
        // 全部播放
        allPlay(newSong){
            this.$root.musicStore.musicID = newSong[0].id;
            this.$root.musicStore.currentMusic = newSong[0];
            this.$root.musicStore.musicList = null;
            this.$root.musicStore.musicList = newSong;
            // 提示窗
            this.$refs.tips.style.display = "block";
            setTimeout( () => {
                this.$refs.tips.style.display = "none";
            },500);
        },
        // 喜欢
        like(){
            this.likeState = !this.likeState;
            let newLike = JSON.parse( window.localStorage.getItem('likePlayList'));
            if(newLike){
                if(this.likeState){
                    newLike.push(this.listID);
                    newLike = Array.from(new Set(newLike));
                }else{
                    let index = newLike.indexOf(this.listID);
                    newLike.splice(index,1);
                }
            }else{
                newLike = [];
                newLike.push(this.listID);
            }
            window.localStorage.setItem('likePlayList',JSON.stringify(newLike));
        },
        // 返回上一级
        back(){
            this.$router.go(-1);
        }
    },
    destroyed() {
        window.removeEventListener("scroll", this.handleScroll, false);
    },
    components:{
        SongItem,
        Loading
    },
}
</script>

<style lang="less" scoped>
.playlist{
    .listDetail{
        position: relative;
        padding: 40px 10px 30px 15px;
        overflow: hidden;
        .top{
            width: 100%;
            background-color: transparent;
            position: absolute;
            top: 6px;
            left: 0;
            z-index: 5;
            span{
                margin: 0px 10px;
                font-size: 24px;
                &:nth-child(1){
                    color: #df3436;
                    float: left;
                }
                &:nth-child(2){
                    color: red;
                    float: right;
                }
                &:nth-child(3){
                    color: white;
                    float: right;
                }
            }
        }
        .playImg{
            height: 20vh;
            background-repeat: no-repeat;
            background-size: cover;
            background-position: center center;
            filter: blur(20px);
            transform: scale(1.5);
            position: absolute;
            left: 0;
            right: 0;
            top: 0;
            bottom: 0;
            z-index: 1;
        }
        .main{
            display: flex;
            position: relative;
            z-index: 2;
            .left{
                width: 126px;
                height: 126px;
                position: relative;
                img{
                    width: 100%;
                    vertical-align: middle;
                }
                .icon{
                    position: absolute;
                    z-index: 3;
                    top: 10px;
                    left: 0;
                    padding: 0 8px;
                    height: 17px;
                    color: #fff;
                    font-size: 9px;
                    text-align: center;
                    line-height: 17px;
                    background-color: rgba(217,48,48,.8);
                    border-top-right-radius: 17px;
                    border-bottom-right-radius: 17px;
                }
                .num{
                    font-style: normal;
                    position: absolute;
                    right: 2px;
                    top: 0;
                    z-index: 3;
                    padding-left: 15px;
                    color: #fff;
                    font-size: 12px;
                    background-position: 0;
                    background-repeat: no-repeat;
                    background-size: 11px 10px;
                    text-shadow: 1px 0 0 rgb(0 0 0 / 15%);
                    background-image: url();
                    &::after{
                        content: "";
                        position: absolute;
                        left: 0;
                        top: 0;
                        width: 100%;
                        height: 18px;
                        z-index: 3;
                        background-image: linear-gradient(90deg,rgba(0,0,0,0),rgba(0,0,0,.2));
                    }
                }   
            }
            .right{
                flex: 1 1 auto;
                width: 1%;
                margin-left: 16px;
                h2{
                    padding-top: 1px;
                    font-size: 17px;
                    line-height: 1.3;
                    color: #fefefe;
                    height: 44px;
                    -webkit-box-pack: center;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    white-space: normal;
                }
                .auth{
                    display: block;
                    position: relative;
                    margin-top: 20px;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    white-space: nowrap;
                    word-break: normal;
                    color: hsla(0,0%,100%,.7);
                    line-height: 1;
                    img{
                        width: 30px;
                        height: 30px;
                        border-radius: 50%;
                        vertical-align: middle;
                        margin-right: 5px;
                    }
                }
                .all{
                    margin-top: 10px;
                    width: 80px;
                    height: 20px;
                    line-height: 20px;
                    color: white;
                    font-size: 12px;
                    background-color: #df3436;
                    text-align: center;
                    border-radius: 10px;
                }
                .tips{
                    display: none;
                    width: 50vw;
                    height: 10vh;
                    line-height: 10vh;
                    text-align: center;
                    position: fixed;
                    top: 0;
                    left: 0;
                    right: 0;
                    bottom: 0;
                    margin: auto;
                    background-color: rgba(255, 0, 0, 0.5);
                }
            }
        }
    }
    .playSong{
        .SongTitle{
            height: 23px;
            line-height: 23px;
            padding: 0 10px;
            font-size: 12px;
            color: #666;
            background-color: #eeeff0;
        }
        ul{
            li{
                div>.musicIndex{
                    text-align: center;
                    display: flex;
                    flex: 1;
                    align-items: center;
                    justify-content: center;
                    width: 40px;
                    height: 55px;
                    font-size: 17px;
                    color: #999;
                }
            }
        }
    }
}
.loadMore{
    padding: 10px 0px;
    text-align: center;
    color: #df3436;
}
</style>